<?php
/**
 * User: @lose
 * Date: 2019-08-26
 * Time: 19:07
 */

namespace Beerus\Modules\DesignerVote\Controllers;

use App\Pockets\GridDataModel as pGridDataModel;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;
use Infinity\cache\designerVote\LeaderBoard as cLeaderBoard;
use Infinity\service\admin\DesignerVote as sDesignerVote;

class IndexController
{
    public function getTotal()
    {
        $state = sDesignerVote::getTotal();
        $headers = ['当前总票数', '设计师/业主 总加票数', '设计师/业主 自然票数', '设计师/业主 今日增加票数', '设计师/业主 今日自然票数', '设计师/业主 昨日增加票数', '设计师/业主 昨日自然票数'];
        $table = new Table($headers, $state);
        return $table->render();
    }


    public function index(Content $content)
    {
        $tab = new Tab();
        $tab->add('设计师排行榜', $this->getVote(cLeaderBoard::TYPE_POPULARITY));
        $tab->add('业主排行榜', $this->getVote(cLeaderBoard::TYPE_OWNER));
        return $content->row($this->getTotal())
            ->row($tab);
    }

    public function expand($info)
    {
        $listAndTotal = sDesignerVote::getUserCaseVote($info['designer_id']);
        $gridModels = new pGridDataModel($listAndTotal['list']['time'], $listAndTotal['total'], [
            'keyName' => 'expand_key'
        ]);

        $listGrid = Admin::grid($gridModels,
            function (Grid $grid) use ($gridModels, $listAndTotal, $info) {
                $grid->paginate($gridModels->getPerPage());
                $grid->disableExport();
                $grid->disableCreateButton();
                $grid->disableRowSelector();
                $grid->disableFilter();
                $grid->disableActions();
                $grid->disableColumnSelector();
                $grid->disablePagination();
                $grid->column('time', '时间')->popup('票数', 'everydaylist');

                foreach ($listAndTotal['list']['designer_case_id'] as $caseId) {
                    $grid->column('num_case_' . $caseId, '案例id' . $caseId . '自然票数');
                    $grid->column('admin_num_case_' . $caseId, '案例id' . $caseId . '手动票数')->display(function ($caseId, $column) {
                        if($this->time == date('Y-m-d') && date('H:i:s') < '12:00:00') {
                            return $column->editable();
                        } else {
                            return $caseId;
                        }
                    });
                }
            });
        return $listGrid->render();
    }

    public static function everydayList(Request $request)
    {
        $key = $request->input('key');
        $key = explode('_', $key);
        $designerId = $key[0];
        $time = $key[1];

        $table = new Table(['时间段', '票数'], sDesignerVote::getVoteNumByDesignerIdAndTime($designerId, $time));
        return $table->render();
    }

    public function getVote($type)
    {
        $listAndTotal = sDesignerVote::index($type);

        foreach ($listAndTotal['list'] as &$info) {
            $info['expand'] = $this->expand($info);
        }

        $gridModel = new pGridDataModel($listAndTotal['list'], [], [
            'keyName' => 'designer_id'
        ]);

        $listGrid = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel, $type) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableCreateButton();
                $grid->disableRowSelector();
                $grid->disableFilter();
                $grid->disableActions();
                $grid->disableColumnSelector();
                $grid->disablePagination();

                $grid->column('designer_id',  $type == cLeaderBoard::TYPE_POPULARITY ? '设计师ID' : '业主ID')->expand(function () {
                    return $this->expand;
                });
                $grid->column('nick', $type == cLeaderBoard::TYPE_POPULARITY ? '设计师名称' : '业主名称');
                if($type == cLeaderBoard::TYPE_OWNER) {
                    $grid->column('bind_uid', '关联设计师ID');
                    $grid->column('bind_nick', '关联设计师名称');
                }

                $grid->column('rank', '榜单排名/榜单总票数')->display(function () {
                    return $this->rank . ' / ' . $this->vote_num;
                });
                $grid->column('now_rank', '实时排名/实时总票数')->display(function () {
                    $rank = $this->rank - $this->now_rank;
                    if ($rank > 0) {
                        return $this->now_rank . "  / " . $this->now_vote_num . " <span class='label label-danger'> +$rank </span>";
                    } else if ($rank < 0) {
                        return $this->now_rank . "  / " . $this->now_vote_num . " <span class='label label-success'> $rank </span>";
                    }
                    return $this->now_rank . ' / ' . $this->now_vote_num;
                });

                $grid->column('today_num', '今日自然增长票数');
                $grid->column('today_admin_num', '今日手动增长票数');
                $grid->column('yest_day_num', '昨日自然票数');
                $grid->column('yest_day_admin_num', '昨日手动票数');
                $grid->column('now_user_vote_num', '实时真实票数');
                $grid->column('now_admin_vote_num', '实时加票数');
            });

        return $listGrid->render();
    }

    public function UpdateNumByCaseId(Request $request, $caseId = 0)
    {
        // 接受caseId
        $num = $request->input('value');
        $name = $request->input('name');
        $caseId = end(explode('_', $name));

        $result = sDesignerVote::UpdateNumByCaseId($caseId, $num);
        if($result) {
            return response()->json([
                'status' => true,
            ]);
        } else {
            return response()->json(['errors' => ['操作失败,请重试']], 422);
        }
    }

    // 临时修改案例票数的功能
    public static function getCase($info)
    {
        $case = sDesignerVote::getCase($info['designer_id']);

        $gridModel = new pGridDataModel($case, [], [
            'keyName' => 'case_id'
        ]);
        $listGrid = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableCreateButton();
                $grid->disableRowSelector();
                $grid->disableFilter();
                $grid->disableActions();
                $grid->disableColumnSelector();
                $grid->disablePagination();
                //  detail 详细数据 使用来foreach的
                $grid->column('designer_id', '设计师id');
                $grid->column('case_id', '案例id');
                date('H:i:s') < '12:00:00' ? $grid->column('num', '手动加票数')->editable() : $grid->column('num', '手动加票数');
            });
        return $listGrid->render();
    }
}